//
//  MPCNetWorkOperation.m
//  MiaoPai
//
//  Created by jiaxuzhou on 2017/5/24.
//  Copyright © 2017年 jiaxuzhou. All rights reserved.
//

#import "MPCNetWorkOperation.h"
#import "MPSNetWorkOperationManager.h"


@interface MPCNetWorkOperation ()


@property (nonatomic, strong) MPBNetWorking *request;

@property (nonatomic, strong) NSDictionary *dicParams;

@property (nonatomic, strong) id uniqueID;

@property (nonatomic, copy) NSString *strUrl;

@property (nonatomic, copy) void (^reqFinishedBlock)(MPBNetWorking *req,MPURLSessionTask *task,id unique);

@property (nonatomic, copy) void (^reqFailedBlock)(MPBNetWorking *req,MPURLSessionTask *task,id unique);

@property (nonatomic, copy) void (^reqCacheBlock)(NSDictionary *cacheData);


@end


@implementation MPCNetWorkOperation


- (void)dealloc
{
    NSLog(@"======>>> %s", __func__);
}

+ (instancetype)operationWithparams:(NSDictionary *)dicParams
                                url:(NSString *)strUrl
                           uniqueId:(id)unique
                            success:(void (^)(MPBNetWorking *req,MPURLSessionTask *task,id unique))reqFinishedBlock
                             failed:(void (^)(MPBNetWorking *req,MPURLSessionTask *task,id unique))reqFailedBlock
{
    MPCNetWorkOperation *operation = [[MPCNetWorkOperation alloc] init];
    operation.dicParams = dicParams;
    operation.strUrl = strUrl;
    operation.uniqueID = unique;
    operation.reqFinishedBlock = reqFinishedBlock;
    operation.reqFailedBlock = reqFailedBlock;
    
    return operation;
}

- (MPBNetWorking *)request
{
    if (_request == nil)
    {
        _request = [[MPBNetWorking alloc] init];
    }
    
    return _request;
}


- (void)main
{
    @autoreleasepool
    {
        
        if (self.isCancelled)
        {
            [self cancel];
        }
        
        __weak typeof(self) weakSelf = self;
        
        
        self.request.uniqueID = self.uniqueID;
        
        [self.request netWorkSyncWithMethod:MPE_POST
                                        URL:self.strUrl
                                     params:self.dicParams
                           isShowNoNetAlert:NO
                                  isEncrypt:NO
                                    success:^(MPBNetWorking *netWork, id responseObject)
         {
             void (^reqFinishedBlock)(MPBNetWorking *req,MPURLSessionTask *task,id unique) = weakSelf.reqFinishedBlock;
             
             if (reqFinishedBlock)
             {
                 reqFinishedBlock(weakSelf.request,nil,weakSelf.uniqueID);
             }
         }
                                     failed:^(MPBNetWorking *netWork, id responseObject)
         {
             void (^reqFailedBlock)(MPBNetWorking *req,MPURLSessionTask *task,id unique) = weakSelf.reqFailedBlock;
             if (reqFailedBlock)
             {
                 reqFailedBlock(weakSelf.request,nil,weakSelf.uniqueID);
             }
         }
                                     cached:^(NSDictionary *cachedObject)
        {
             
             void (^reqCacheBlock)(NSDictionary *cacheData) = weakSelf.reqCacheBlock;
             if (reqCacheBlock)
             {
                 reqCacheBlock(cachedObject);
             }
         }];
    }
}


@end
